<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Image;

class Index extends Controller
{
    public function index(){
        return view();
    }

    public function upimg(){
        $dirName = "uploads" . DS . 'images' . DS . (date('Ymd'));
        $saveDir = ROOT_PATH . "public" . DS . $dirName;
        if (!file_exists($saveDir)){
            mkdir($saveDir,0777,true);
        }
        $file = request()->file('file');
        if($file){
            $info = $file->rule("get_rand")->move($saveDir);
             $image = \think\Image::open($info->getRealPath());
             $image->crop(300, 300);
            if($info){
                $img_src = DS . $dirName . DS .$info->getFilename();
                $saveName = md5(uniqid(rand(), true)) . '.png';
                $image->save($saveDir . DS .$saveName);
                $thumb_src = DS . $dirName . DS .$saveName;
                return json([
                    'status'=>1,
                    'msg'=>'图片上传成功',
                    'photo'=>$img_src,
                    'thumb'=>$thumb_src
                ]);
            }else{
                $this->error($file->getError());
            }
        }
    }

    public function delImg(){
        if( request()->isAjax() ){
            $photo = input('photo');
            $thumb = input('thumb');
            if( !empty($photo) ){
                @unlink(ROOT_PATH . 'public' . $photo);
                @unlink(ROOT_PATH . 'public' . $thumb);
                return json(['status'=>1,'msg'=>'删除成功']);
            }else{
                return json(['status'=>0,'msg'=>'删除失败']);    
            }
        }
        
    }

}
